package com.example.home.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.component.GameCtrlCom;
import com.example.common.control.ICallback;
import com.example.common.model.GameApiGetGameResp;
import com.example.common.model.GameResp;
import com.example.home.model.PreonlineCard;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

public class HomePreonlineViewModel extends ViewModel {
    // TODO: Implement the ViewModel
    private GameCtrlCom gameCtrlCom;
    public static int LOAD_NUMBER=3;
    public boolean isFirstLoad=true;

    private MutableLiveData<String> remainMsg=new MutableLiveData<>();//提示消息
    private MutableLiveData<Boolean> LTorLD=new MutableLiveData<>();//true为下滑加载完成，false为上滑加载完成
    private LinkedList<GameResp> games=new LinkedList<>();//游戏数据
    private MutableLiveData<String> date=new MutableLiveData<>();//顶部时间展示

    private SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
    private SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM月dd日", Locale.getDefault());


    public HomePreonlineViewModel(){
        initData();
    }

    /**
     * 数据初始化
     */
    private void initData(){
        gameCtrlCom=new GameCtrlCom();
    }

    /**
     * 下滑加载顶部图片
     */
    public void loadTop(){
        gameCtrlCom.getGameRecommend(new ICallback() {
            @Override
            public void onSuccess(Object response) {
                if(response!=null){
                    List<GameResp> resp=(List<GameResp>)response;
                    games.addAll(0,resp);
                    //加载了多少个
                    LOAD_NUMBER=resp.size();
                    LTorLD.setValue(true);
                }
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    /**
     * 上滑更新
     */
    public void loadDown(){
        gameCtrlCom.getGameRecommend(new ICallback() {
            @Override
            public void onSuccess(Object response) {
                if(response!=null){
                    List<GameResp> resp=(List<GameResp>)response;
                    games.addAll(resp);
                    //加载了多少个
                    LOAD_NUMBER=resp.size();
                    LTorLD.setValue(false);
                }
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    /**
     * 设置顶部时间条
     * @param position 滑动到的位置
     */
    public void setTopDate(int position) throws ParseException {
        if(games.get(position).getStat().getUpdateTime()!=null){
            Date MyDate= simpleDateFormat1.parse(games.get(position).getStat().getUpdateTime());
            String dateStr=simpleDateFormat2.format(MyDate);
            date.setValue(dateStr);
        }
    }

    public MutableLiveData<String> getDate() {
        return date;
    }

    public LinkedList<GameResp> getGames() {
        return games;
    }

    public MutableLiveData<Boolean> getLTorLD() {
        return LTorLD;
    }

}